﻿/*
 * 2016年7月11日 06:54:04
 * 门派模式（选择门派）
 */

        var status;
var p;
var eventid = 16711;//活動ID
var eff = "#fEffect/CharacterEff/1051296/1/0#";

var rewardItem = 4000000;  // 道具id
var rewardQty = 1;// 数量

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == 0) {
        cm.dispose();
        return;
    } else if (mode == 1) {
        status++;
    } else {
        status--;
    }
    p = new EvPoint();
    if (p.getEventPoints(eventid, cm.getPlayer().getId()) >= 1) {
        cm.dispose();
        cm.openNpc(9330084, "mpa");
        return;
    } else if (status == 0) {
        var text = "- #e#d门派系统：#n\r\n\r\n"
        text += "申请加入门派后即不可更换，请选择好门派后申请，门派每日成员福利，老大福利，各派老大每日可在门派基地申请一次召唤一只奖励BOSS，打死可获得大量福利，另外门派成员可获得强大属性的霸气勋章，门派的福利在于门派贡献度。以及门派成员活跃。您准备好了加入吗？\r\n#r(PS：加入门派后则打开管理门派界面。可查看门派成员活跃度信息、领取福利、进入基地等界面。)\r\n\r\n"
        text += "#L1##b" + eff + " #t1142745##l\t";//#L xx # 這裏面的數字不能改哦
        text += "#L2##b" + eff + " #t1142746##l\r\n";
        text += "#L3##r" + eff + " 随机加入门派！（有勇敢奖励哦！）#l\r\n ";
        cm.sendSimple(text)
    } else if (status == 1) {
        if (selection == 3) { // 随机加入的
            cm.sendNext("这项操作将随机加入到一个已有的门派里面，你确定吗？");
        } else {
            p = new EvPoint();
            if (p.getEventPoints(eventid, cm.getPlayer().getId()) >= 1) {
                cm.sendOk("你好像已經加入了門派了哦！！\r\n你的門派是:" + p.getMenpaiName() + "!\r\n請繼續為你的門派加油喔！！！");
                cm.dispose();
                return;
            }
            if (p.AddMenpaiMember(selection)) {
				cm.warp(50000);
                cm.sendOk("#b申请加入成功！\r\n你加入的门派是：#r" + p.getMenpaiName() + "#b!\r\n每天可以通过个人贡献度来提高门派繁荣度哦。");
                cm.dispose();
            } else {
                cm.sendOk("你好像已经加入了门派了哦！！\r\n你加入的门派是:" + p.getMenpaiName() + "!\r\n请继续为你的门派做贡献喔！！！");
                cm.dispose();
            }
        }
    } else if (status == 2) {
        var pass = true;
        for (var i = 1; i < 4; i++) {
            if (cm.getSpace(i) < 2) {
                pass = false;
                break;
            }
        }
        if (pass) {
            var rand = Math.floor(Math.random() * 2) + 1;
            if (p.AddMenpaiMember(rand)) {
                cm.sendOk("#b申请加入成功！\r\n你加入的门派是：#r" + p.getMenpaiName() + "#b!\r\n每天可以通过个人贡献度来提高门派繁荣度哦。");
                cm.gainItem(rewardItem,rewardQty);
				cm.warp(50000);
                cm.dispose();
            } else {
                cm.sendOk("你好像已经加入了门派了哦！！\r\n你加入的门派是:" + p.getMenpaiName() + "!\r\n请继续为你的门派做贡献喔！！！");
                cm.dispose();
            }
        } else {
            cm.sendOk("你好想没有足够的背包栏喔！\r\n请让你的背包腾出2个以上的格子.");
            cm.dispose();
        }
    }
}

/*
 - menpaiMember
 id,mpid,charid,charName,points,type,date
 id - index
 mpid - 外鍵mpid
 mpid = 1  飞龙在天
 mpid = 2  虎啸九天
 charid - 角色id
 charname - 角色名字
 points - 角色貢獻度
 type - 角色在門派中扮演的角色
 type = 0 普通成員
 type = 1 門派老大
 date - 角色門派加入時間
 */
var EvPoint = function () {
    //用法：var P = new EvPoint();
    //P.函數名字(),如：P.AddMenpaiMemberPoints(增加的貢獻度)
    this.AddMenpaiMemberPoints = function (points) {//增加門派角色的貢獻度（可負號扣除）
        var data = cm.getConnection().prepareStatement("SELECT * FROM menpaiMember where charid = " + cm.getPlayer().getId() + "").executeQuery(); // 查询数据
        if (!data.next()) {
            cm.playerMessage("增加貢獻度失敗，請先加入一個門派。");
        }
        var update = cm.getConnection().prepareStatement("update menpaiMember set points = ? where charid = " + cm.getPlayer().getId() + "");//更新为已使用
        update.setString(1, this.getMenpaiMemberPoints() + points);
        update.executeUpdate();
        update.close();
    }
    this.getMenpaiMemberPoints = function () {//得到門派角色的貢獻度
        var data = cm.getConnection().prepareStatement("SELECT points FROM menpaiMember where charid = " + cm.getPlayer().getId() + "").executeQuery(); // 查询数据
        if (data.next()) {
            return data.getInt("points");
        }
    }
    this.AddMenpaiMember = function (mpid) {//在某個門派中增加一個成員
        var data = cm.getConnection().prepareStatement("SELECT * FROM menpaiMember where charid = " + cm.getPlayer().getId() + "").executeQuery(); // 查询数据
        if (data.next()) {
            return false;
        }
        var insert = cm.getConnection().prepareStatement("INSERT INTO menpaiMember VALUES(?,?,?,?,?,?,?)"); // 载入数据
        insert.setString(1, null); //载入记录ID
        insert.setString(2, mpid); //载入mpid
        insert.setString(3, cm.getPlayer().getId());//cid
        insert.setString(4, cm.getPlayer().getName());//cname
        insert.setString(5, 0);//points 点数
        insert.setString(6, 0);// 角色在門派中扮演的角色
        insert.setString(7, null);//角色門派加入時間
        insert.executeUpdate(); //更新
        insert.close();
        this.setEventPoints(eventid, cm.getPlayer().getId(), 1);
        return true;
    }
    this.getMenpaiName = function () {//得到角色門派的名字
        var i = 0;
        var data = cm.getConnection().prepareStatement("SELECT mpid FROM menpaiMember where charid = " + cm.getPlayer().getId() + "").executeQuery(); // 查询数据
        while (data.next()) {
            i = data.getInt("mpid");//得到門派id
        }
        if (i == 1) {
            return "飞龙在天";
        } else if (i == 2) {
            return "虎啸九天";
        } else {
            return i;
        }
    }
    this.getMenpaiId = function () {//得到角色門派的ID
        var i = 0;
        var data = cm.getConnection().prepareStatement("SELECT mpid FROM menpaiMember where charid = " + cm.getPlayer().getId() + "").executeQuery(); // 查询数据
        while (data.next()) {
            i = data.getInt("mpid");//得到門派id
        }
        return i;
    }

    // ----- 分割線（下面是活動需要函數） ------
    this.DelEventPoints = function (Eventid, charid) {
        var delectData = cm.getConnection().prepareStatement("delete from EventTimes where eventid = " + Eventid + " and cid = " + charid + "");
        delectData.executeUpdate(); //删除数据
    }

    this.DelEventPoints = function (Eventid, charid) {
        var delectData = cm.getConnection().prepareStatement("delete from EventTimes where eventid = " + Eventid + " and cid = " + charid + "");
        delectData.executeUpdate(); //删除数据
    }

    this.getEventTimes = function (Eventid, charid) {//通过eventid来得到参与这个活动的次数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i = Times.getString("times");//得到次数
        }
        return parseInt(i);
    }

    this.getEventPoints = function (Eventid, charid) {//通过eventid来得到参与这个活动的点数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i = Times.getString("points");//得到点数
        }
        return parseInt(i);
    }

    this.setEventPoints = function (Eventid, charid, points) {//通过eventid来给予参与这个活动的点数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i++;
        }
        if (i == 0) {//insert
            var insert = cm.getConnection().prepareStatement("INSERT INTO EventTimes VALUES(?,?,?,?,?,?,?)"); // 载入数据
            insert.setString(1, null); //载入记录ID
            insert.setString(2, Eventid); //载入活动ID
            insert.setString(3, cm.getPlayer().getId());//cid
            insert.setString(4, cm.getPlayer().getName());//cname
            insert.setString(5, points);//points 点数
            insert.setString(6, this.getEventTimes(1, charid));//times 次数
            insert.setString(7, null);//
            insert.executeUpdate(); //更新
        } else {//update
            var update = cm.getConnection().prepareStatement("update EventTimes set points = ? where eventid = " + Eventid + " and cid = " + charid + "");//更新为已使用
            update.setString(1, this.getEventPoints(Eventid, charid) + points);
            update.executeUpdate();
        }
    }

    this.setEventTimes = function (Eventid, charid, times) {//通过eventid来设置参与这个活动的次数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i++;
        }
        if (i == 0) {//insert
            var insert = cm.getConnection().prepareStatement("INSERT INTO EventTimes VALUES(?,?,?,?,?,?,?)"); // 载入数据
            insert.setString(1, null); //载入记录ID
            insert.setString(2, Eventid); //载入活动ID
            insert.setString(3, cm.getPlayer().getId());//cid
            insert.setString(4, cm.getPlayer().getName());//cname
            insert.setString(5, this.getEventPoints(2, charid));//points 点数
            insert.setString(6, times);//times 次数
            insert.setString(7, null);//
            insert.executeUpdate(); //更新
        } else {//update
            var update = cm.getConnection().prepareStatement("update EventTimes set times = ? where eventid = " + Eventid + " and cid = " + charid + "");//更新为已使用
            update.setString(1, this.getEventTimes(Eventid, charid) + times);
            update.executeUpdate();
        }
    }
}
